from django.shortcuts import render
from .models import Article,Category,Tag
from frontuser.models import FrontUser,UserExtension
from django.http import HttpResponse

# Create your views here.

def index(request):
    # category = Category(name="最新文章")
    # category.save()
    # article = Article(title="好书",content="三天")
    # article.category = category
    # article.save()
    article = Article.objects.first()
    print(article.category.name)
    return HttpResponse('success')

def delete_view(request):
    category = Category.objects.get(pk=2)
    category.delete()
    return HttpResponse('DELETE SUCCESS')

def one_to_one_view(request):
    # user = FrontUser(username='kangbazi')
    # user.save()
    # extension = UserExtension(school='清华')
    # extension.user = user
    # extension.save()
    #
    # extension = UserExtension.objects.first()
    # print(extension.user)
    user = FrontUser.objects.first()
    print(user.extension)
    return HttpResponse("ok")

def one_to_many_view(request):
    # article = Article(title="我真的好怕你",content="为什么？因为我怕老婆")
    # category = Category.objects.first()
    # author = FrontUser.objects.first()
    # article.category = category
    # article.author = author
    # article.save()

    #category = Category.objects.get(pk=1)
    # article = category.article_set.first()
    #articles = category.article.all()
    # for article in articles:
    #     #     print(article)
    #哪些分类下面 有标题包含好书的文章
    categorys = Category.objects.filter(article__title='好书')
    print(categorys)



    return HttpResponse(" one to many ok")

def many_to_many_view(request):
    # article = Article.objects.first()
    # tag = Tag(name="高亮")
    # tag.save()
    # article.tags.add(tag)
    article = Article.objects.get(pk=1)
    tags = article.tags.all()
    for tag in  tags:
        print(tag)
    return HttpResponse(" many to many ok")

